using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Drawing;
using Alogient.Cameleon.Forms.Sdk;
using Alogient.Cameleon.Forms.Sdk.Items;
using Alogient.Cameleon.Sdk;
using System.Collections;

namespace Alogient.Cameleon.Forms.Sdk.Controls
{
    internal class FormRadio : CMFormElement
    {
        protected const string OTHER_VALUE = "__OTHER";
        protected const string OTHER_TEXTBOX_ID_FORMAT = "Other_Textbox_{0}";
        public override string Value
        {
            get
            {
                RadioButtonList radios = this.FindControl(string.Format("radios_{0}", this.ID)) as RadioButtonList;

                if (radios.SelectedValue == OTHER_VALUE)
                {
                    // Get Textbox value
                    TextBox otherTextbox = this.FindControl(string.Format(OTHER_TEXTBOX_ID_FORMAT, this.ID)) as TextBox;
                    return otherTextbox.Text;
                }
                
                return radios.SelectedValue;
            }
        }
        public override string[] Values
        {
            get
            {
                RadioButtonList radios = this.FindControl(string.Format("radios_{0}", this.ID)) as RadioButtonList;

                ArrayList list = new ArrayList();

                if (radios.SelectedValue == OTHER_VALUE)
                {
                    //Get Textbox value
                    TextBox otherTextbox = this.FindControl(string.Format(OTHER_TEXTBOX_ID_FORMAT, this.ID)) as TextBox;
                    list.Add(otherTextbox.Text);
                }
                else
                {
                    list.Add(radios.SelectedValue);
                }

                return list.ToArray(typeof(string)) as string[];
            }
        }
        internal FormRadio(Forms.Sdk.Items.Radio element): base(element)
        {
            PlaceHolder radioPh = new PlaceHolder();

            System.Web.UI.WebControls.RadioButtonList radios = new RadioButtonList();
            radios.ID = string.Format("radios_{0}", this.ID);
            foreach (FormElementChoice choice in element.Choices)
            {
                if ((CMServices.IsLive && choice.VisibleLive) || (!CMServices.IsLive && choice.VisiblePreview))
                {
                    ListItem radio = new ListItem();
                    radio.Text = choice.Name;
                    radio.Value = choice.ID.ToString();// Value;

                    if (choice.ID == element.DefaultValue)
                        radio.Selected = true;

                    radios.Items.Add(radio);
                }
            }

            radioPh.Controls.Add(radios);

            if (element.Other)
            {
                ListItem radio = new ListItem();

                radio.Text = element.OtherLabel;
                radio.Value = OTHER_VALUE;

                radios.Items.Add(radio);

                TextBox otherTextbox = new TextBox();
                otherTextbox.ID = string.Format(OTHER_TEXTBOX_ID_FORMAT, this.ID);

                radioPh.Controls.Add(otherTextbox);
            }

            Label label = new Label();
            label.Text = element.Label;
            label.CssClass = string.Format("{0}Label", element.CSS);

            #region Validators

            PlaceHolder phValidators = new PlaceHolder();

            if (element.Required)
            {
                Label redStar = new Label();
                redStar.ForeColor = Color.Red;
                redStar.Text = "*";

                RequiredFieldValidator required = new RequiredFieldValidator();
                required.ID = string.Format("required_{0}_{1}", element.FormID, element.ID);
                required.ControlToValidate = radios.ID;
                required.ValidationGroup = element.FormID.ToString();
                required.Display = ValidatorDisplay.Dynamic;
                required.ErrorMessage = string.Format(Alogient.Cameleon.Forms.Sdk.Util.WebUtil.GetValidatorRessource("Required"), element.Label);
            
                phValidators.Controls.Add(redStar);
                phValidators.Controls.Add(required);
            }

            #endregion

            GenerateRow(element, label, radioPh, phValidators);
        }
    }
}
